package com.iwhalecloud.citybrain.flow.platform.manager.application.utils;

import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.common.impl.el.ExpressionManager;
import org.flowable.engine.common.impl.interceptor.CommandContext;
import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.flowable.engine.impl.util.CommandContextUtil;

/**
 * @author wuliepeng
 */
public class ExpressionUtils {

    public static boolean isExpression(String source) {
        String expressionPrefix = "${";
        String expressionSuffix = "}";
        if (StringUtils.isNotEmpty(source) && source.startsWith(expressionPrefix) && source.endsWith(expressionSuffix)) {
            return true;
        }
        return false;
    }

    /**
     * 获取表达式内容，${expression} => expression
     *
     * @param source
     * @return
     */
    public static String trimExpression(String source) {
        if (isExpression(source)) {
            return source.substring(2, source.length() - 1);
        }
        return source;
    }

    /**
     * 获取表达式管理器，用于解析表达式
     *
     * @return
     */
    public static ExpressionManager getExpressionManager() {
        CommandContext commandContext = CommandContextUtil.getCommandContext();
        ProcessEngineConfigurationImpl processEngineConfiguration = CommandContextUtil.getProcessEngineConfiguration(commandContext);
        return processEngineConfiguration.getExpressionManager();
    }
}
